//
// Dr. Ing. h.c. F. Porsche AG confidential. This code is protected by intellectual property rights.
// The Dr. Ing. h.c. F. Porsche AG owns exclusive legal rights of use.
//

#import "StorageManager.h"

@interface StorageManager ()
@property(nonatomic,strong) NSString * cachesPath;
@end
@implementation StorageManager

+(instancetype) shared {
    static StorageManager * storageManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!storageManager) {
            storageManager = [StorageManager new];
        }
    });
    return storageManager;
}

-(NSString *)cachesPath {
    if (!_cachesPath) {
        _cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    }
    return _cachesPath;
}

-(void) save:(NSDictionary *) dicData fileName:(NSString *) fileName {
    NSString * path = [NSString stringWithFormat:@"%@/%@.plist",self.cachesPath,fileName];
    NSURL * url = [NSURL fileURLWithPath:path];
    if (!url) {
        return;
    }
    NSError * error;
    [dicData writeToURL:url error:&error];
    if (error) {
        NSLog(@"%@",error);
    }
}

-(NSDictionary *) readWithFileName:(NSString *) fileName {
    NSString * path = [NSString stringWithFormat:@"%@/%@.plist",self.cachesPath,fileName];
    
    NSURL * url = [NSURL fileURLWithPath:path];
    if (!url) {
        return nil;
    }
    return [NSDictionary dictionaryWithContentsOfURL:url];
}

@end
